package org.zxy.auth.controller;

import org.zxy.auth.service.IPermissionService;
import org.zxy.auth.domain.Permission;
import org.zxy.auth.query.PermissionQuery;
import org.zxy.base.annotation.RonghuaPermission;
import org.zxy.base.util.AjaxResult;
import org.zxy.base.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission" )
@RonghuaPermission(name = "权限管理")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;


    /**
     * 保存和修改
     * @param permission 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @RonghuaPermission(name="查询所有员工")
    public AjaxResult insertOrUpdate(@RequestBody Permission permission) {
        try {
            if ( permission.getId() != null)
                permissionService.update(permission);
            else
                permissionService.insert(permission);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！" + e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}" )
    @RonghuaPermission(name="根据Id删除员工")
    public AjaxResult deleteById(@PathVariable("id" ) Long id) {
        try {
                permissionService.deleteById(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除对象失败！" + e.getMessage());
        }
    }
    /**
     * 根据Id查询对象信息
     * @param
     * @return
     */
    //获取用户
    @GetMapping("/{id}" )
    @RonghuaPermission(name="根据Id查询员工")
    public AjaxResult selectById(@PathVariable("id" ) Long id) {
        try {
            Permission permission =permissionService.selectById(id);
            return AjaxResult.success(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！" + e.getMessage());
        }
    }


    /**
    * 查看所有对象信息
    * @return
    */
    @GetMapping
    @RonghuaPermission(name="查询所有员工")
    public AjaxResult selectAll() {
        try {
            List< Permission> list = permissionService.selectAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！" + e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @RonghuaPermission(name="员工分页查询")
    public AjaxResult queryPage(@RequestBody PermissionQuery query) {
        try {
            PageList<Permission> pageList = permissionService.queryPage(query);
            return AjaxResult.success(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！" + e.getMessage());
        }
    }

    /**
     * 批量删除对象
     *
     * @param
     * @return
     */
    @PatchMapping
    @RonghuaPermission(name="员工批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
                permissionService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败！" + e.getMessage());
        }
    }
    @GetMapping("/tree")
    public AjaxResult tree(){
        try {
            List< Permission> list = permissionService.queryTree();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询权限树失败" + e.getMessage());
        }
    }

}
